//
//  ExUIViewController.swift
//  todoApp
//
//  Created by wangyaqing on 15/3/6.
//  Copyright (c) 2015年 ll studio. All rights reserved.
//

import Foundation

public extension UIViewController
{
    //获取当前服务器所备份的数据
    func downLoadNotesFromAVOS(nameString : NSString?) -> NSArray?
    {
        
        self.showIndicatorWithContent("请稍后")
        if let localName = nameString
        {
            var avosQuery = AVQuery(className: "NoteClass");
            avosQuery.whereKey("userId", equalTo:nameString);
            
            var results :[AVObject]? = avosQuery.findObjects() as! [AVObject]?;
            self.hideIndicator()
            
            if (results==nil || results?.count==0)
            {
                return [];
            }
            var tmp = NSMutableArray(array: results!)
            
            //过滤掉没有noteId的脏数据
            for index in results!
            {
                var noteId:NSString? = index.objectForKey("noteId") as! NSString?
                if (noteId == nil || noteId!.length == 0)
                {
                    tmp.removeObject(index)
                }
            }
            
            return tmp
        }
        else
        {
            return nil
        }
    }
    
    //验证账号是否已经存在
    func alreadyExistAccount(email:NSString!) -> Bool{
        self.showIndicatorWithContent("请稍后")
        
        var avosQuery = AVQuery(className: "AccountClass");
        avosQuery.whereKey("userAccount", equalTo:email);
        
        var results :[AVObject]? = avosQuery.findObjects() as! [AVObject]?;
        
        self.hideIndicator()
        
        if (results==nil || results?.count==0)
        {
            //不存在此邮箱
            return false;
        }
        else
        {
            //已存在此账号
            return true;
        }
    }
    


    func userId()->NSString?
    {
        return NSUserDefaults.standardUserDefaults().stringForKey("userAccount")
    }
    
    func startUploadData(index:Int!, uploadArray:NSArray!)->Void
    {
        if (self.userId() == nil)
        {
            return
        }
        if (index > uploadArray.count-1)
        {
           // JDStatusBarNotification.showWithStatus("同步成功", dismissAfter: 1)
            self.showHudWithTitle("已经同步到服务器")
            return
        }
        
        var dict: NSDictionary = uploadArray.objectAtIndex(index) as! NSDictionary
        
        var avosObj = dict.objectForKey("avosObj") as! AVObject
        var noteObj = dict.objectForKey("noteObj") as! NoteModel
        
        avosObj.setObject(noteObj.noteText, forKey: "noteText")
        avosObj.setObject(noteObj.noteTag, forKey: "noteTag")
        avosObj.setObject(noteObj.noteId, forKey: "noteId")
        avosObj.setObject(noteObj.userId, forKey: "userId")
        avosObj.setObject(NSNumber(double: noteObj.localCreateTime), forKey: "localCreateTime")

        if (avosObj.save())
        {
            self.startUploadData(index+1, uploadArray: uploadArray)
        }
    }
    
    func getCreateTimeElapse(createTimeFrom1970 :NSTimeInterval) -> NSString!
    {
        var now = NSDate()
        var nowInterval = now.timeIntervalSince1970
        
        var elapseInterval = nowInterval - createTimeFrom1970
        var retStr : NSString = ""
        
        if (elapseInterval / 3600 < 1)
        {
            if (elapseInterval/60 < 1)
            {
                return "1分钟内"
            }
            else
            {
                retStr = NSString(format: "%f", elapseInterval/60)
                retStr = retStr.substringToIndex(retStr.length - 7)
                retStr = NSString(format: "%@分钟前", retStr)
            }
        }
        else if((elapseInterval/3600>1) && (elapseInterval/3600<24))
        {
            retStr = NSString(format: "%f", elapseInterval/3600)
            retStr = retStr.substringToIndex(retStr.length - 7)
            retStr = NSString(format: "%@小时前", retStr)
        }
        else if( (elapseInterval/86400>1) && (elapseInterval/86400<30))
        {
            retStr = NSString(format: "%f", elapseInterval/86400)
            retStr = retStr.substringToIndex(retStr.length - 7)
            retStr = NSString(format: "%@天前", retStr)
        }
        else if((elapseInterval/2592000>1) && (elapseInterval/2592000<12))
        {
            retStr = NSString(format: "%f", elapseInterval/2592000)
            retStr = retStr.substringToIndex(retStr.length - 7)
            retStr = NSString(format: "%@月前", retStr)
        }
        else
        {
            var createDate = NSDate(timeIntervalSince1970: createTimeFrom1970)
            
            var formatter:NSDateFormatter = NSDateFormatter()
            
            retStr = formatter.stringFromDate(createDate)
        }
    
        return retStr
    }
    
}



       